<?php
/* --------------------------------------------------------------
   PropertiesAdminView.php 2012-02-14 gambio
   Gambio GmbH
   http://www.gambio.de
   Copyright (c) 2011 Gambio GmbH
   Released under the GNU General Public License (Version 2)
   [http://www.gnu.org/licenses/gpl-2.0.html]
   --------------------------------------------------------------
*/


class PropertiesAdminControl 
{
	function PropertiesAdminControl() 
	{
	}
	
	function get_all_properties_ids()
	{
		$coo_properties_data_agent = MainFactory::create_object('PropertiesDataAgent');
		$t_properties_ids_array = $coo_properties_data_agent->get_all_properties_ids();
		
		return $t_properties_ids_array;
	}
	
	function get_properties_full_struct($p_properties_id, $p_language_id=false, $p_prop_values=true)
	{
		$coo_properties_data_agent = MainFactory::create_object('PropertiesDataAgent');
		$t_full_struct = $coo_properties_data_agent->get_properties_full_struct(
														$p_properties_id, 
														$p_language_id, 
														$p_prop_values
													);
		return $t_full_struct;
	}
	
	function save($p_type, $p_data_array)
	{
		switch($p_type)
		{
			case 'properties':
				$t_saved_id = $this->save_properties($p_data_array);
				break;
				
			case 'properties_values':
				$t_saved_id = $this->save_properties_values($p_data_array);
				break;
				
			default:
				trigger_error('unknown save_type: '. $p_type, E_USER_ERROR);
		}
		
		if($t_saved_id < 1) 
		{
			trigger_error('invalid t_saved_id returned: '. $t_saved_id, E_USER_ERROR);
		}
		return (int)$t_saved_id;
	}

	
	function delete($p_type, $p_data_array)
	{
		switch($p_type)
		{
			case 'properties':
				$this->delete_properties($p_data_array['properties_id']);
				break;
				
			case 'properties_values':
				$this->delete_properties_values($p_data_array['properties_values_id']);
				break;
				
			default:
				trigger_error('unknown save_type: '. $p_type, E_USER_ERROR);
		}
	}
	
	
	function save_properties($p_data_array)
	{
		$t_insert_mode = true;
		
		if(empty($p_data_array['properties_id']) == false) 
		{
			# switch to update-mode, if properties_id exists
			$t_insert_mode = false;
		}
		
		$coo_properties_do = new GMDataObject('properties');
		$t_properties_id = 0;
		
		if($t_insert_mode) {
			# set to false for an insert
			$coo_properties_do->set_keys(array('properties_id' => false));
		} 
		else {
			# using a given key will cause an update
			$t_properties_id = (int)$p_data_array['properties_id']; 
			$coo_properties_do->set_keys(array('properties_id' => $t_properties_id));
		}
		$coo_properties_do->set_data_value('sort_order', $p_data_array['properties_sort_order']);
		$t_insert_id = $coo_properties_do->save_body_data();
		if($t_insert_id > 0) $t_properties_id = $t_insert_id;
		
		#properties_description table
		$t_languages_array = xtc_get_languages();
		
		for($i=0; $i<sizeof($t_languages_array); $i++) 
		{
			$coo_properties_descr_do = new GMDataObject('properties_description');
			
			$t_language_id 	 = $t_languages_array[$i]['id'];
			$t_language_code = $t_languages_array[$i]['code'];
			
			if($t_insert_mode) {
				# set to false for an insert
				$coo_properties_descr_do->set_keys(array('properties_description_id' => false));
				
				# save foreign keys
				$coo_properties_descr_do->set_data_value('properties_id', $t_properties_id);
				$coo_properties_descr_do->set_data_value('language_id', $t_language_id);
			} 
			else {
				# using a given key will cause an update
				$coo_properties_descr_do->set_keys(
												array(
													'properties_id' => $t_properties_id,
													'language_id'	=> $t_language_id
												)
											);
			}
			
			$coo_properties_descr_do->set_data_value('properties_name', xtc_db_prepare_input($p_data_array['properties_name'][$t_language_code]));
                        $coo_properties_descr_do->set_data_value('properties_admin_name', xtc_db_prepare_input($p_data_array['properties_admin_name'][$t_language_code]));
			$t_properties_descr_id = $coo_properties_descr_do->save_body_data();
		}
		return $t_properties_id;
	}
	
	function delete_properties($p_properties_id)
	{
		$c_properties_id = (int)$p_properties_id;
		
		# get related properties_values 
		$coo_data = new GMDataObjectGroup('properties_values', array('properties_id' => $c_properties_id));
		$coo_data_array = $coo_data->get_data_objects_array();
		
		# delete related properties_values
		for($i=0; $i<sizeof($coo_data_array); $i++)
		{
			$this->delete_properties_values($coo_data_array[$i]->get_data_value('properties_values_id') );
		}
		
		# delete properties_description
		$coo_data_object = new GMDataObject('properties_description');
		$coo_data_object->set_keys(array('properties_id' => $c_properties_id));
		$coo_data_object->delete();
		
		# delete properties
		$coo_data_object = new GMDataObject('properties');
		$coo_data_object->set_keys(array('properties_id' => $c_properties_id));
		$coo_data_object->delete();
	}
	
	function delete_properties_values($p_properties_values_id)
	{
		$c_properties_values_id = (int)$p_properties_values_id;
		
		# delete properties_values
		$coo_data_object = new GMDataObject('properties_values');
		$coo_data_object->set_keys(array('properties_values_id' => $c_properties_values_id));
		$coo_data_object->delete();
		
		# delete properties_values_description
		$coo_data = new GMDataObject('properties_values_description');
		$coo_data->set_keys(array('properties_values_id' => $c_properties_values_id));
		$coo_data->delete();
		
		# delete products_properties_combis_values
		$coo_data = new GMDataObjectGroup('products_properties_index', array('properties_values_id' => $c_properties_values_id));
		$coo_data_array = $coo_data->get_data_objects_array();

		$coo_properties_combis_admin_control = MainFactory::create_object('PropertiesCombisAdminControl');

		# delete related properties_combis
		for($i=0; $i<sizeof($coo_data_array); $i++)
		{
			$coo_properties_combis_admin_control->delete_properties_combis(array($coo_data_array[$i]->get_data_value('products_properties_combis_id')));
			$coo_data_array[$i]->delete(); #delete index entry
		}

	}
	
	function save_properties_values($p_data_array)
	{
		$t_insert_mode = true;
		$coo_properties_values_do = new GMDataObject('properties_values');
		$t_properties_values_id = 0;
		
		if(empty($p_data_array['properties_values_id']) == false) {
			# using a given key will cause an update
			$t_properties_values_id = (int)$p_data_array['properties_values_id']; 
			$coo_properties_values_do->set_keys(array('properties_values_id' => $t_properties_values_id));
			$t_insert_mode = false;			
		} 
		else {
			# set to false for an insert
			$coo_properties_values_do->set_keys(array('properties_values_id' => false));
		}
		$coo_properties_values_do->set_data_value('properties_id', (int)$p_data_array['properties_id']);
		$coo_properties_values_do->set_data_value('sort_order',    (int)$p_data_array['sort_order']);
		$coo_properties_values_do->set_data_value('value_model', 		$p_data_array['value_model']);
		$coo_properties_values_do->set_data_value('value_price_type', 	$p_data_array['value_price_type']);
		$coo_properties_values_do->set_data_value('value_price', (float)$p_data_array['value_price']);
		
		$t_insert_id = $coo_properties_values_do->save_body_data();
		if($t_insert_id > 0) $t_properties_values_id = $t_insert_id;
		
		#properties_description table
		$t_languages_array = xtc_get_languages();
		
		
		
		
		for($i=0; $i<sizeof($t_languages_array); $i++) 
		{
			$coo_properties_descr_do = new GMDataObject('properties_values_description');
			
			$t_language_id 	 = $t_languages_array[$i]['id'];
			$t_language_code = $t_languages_array[$i]['code'];
			
			if($t_insert_mode) {
				# set to false for an insert
				$coo_properties_descr_do->set_keys(array('properties_values_id' => false));
				
				# save foreign keys
				$coo_properties_descr_do->set_data_value('properties_values_id', $t_properties_values_id);
				$coo_properties_descr_do->set_data_value('language_id', $t_language_id);
			} 
			else {
				# using a given key will cause an update
				$coo_properties_descr_do->set_keys(
												array(
													'properties_values_id' => $t_properties_values_id,
													'language_id'	=> $t_language_id
												)
											);
			}
			
			$coo_properties_descr_do->set_data_value('values_name', xtc_db_prepare_input($p_data_array['properties_values_name'][$t_language_code]));
			$t_properties_descr_id = $coo_properties_descr_do->save_body_data();
		}
		return $t_properties_descr_id;
	}
	
	function get_properties_in_combis_count($p_properties_id){
		$t_properties_id = (int)$p_properties_id;
		
		$coo_data = new GMDataObjectGroup('products_properties_index', array('properties_id' => $t_properties_id, 'language_id' => $_SESSION['languages_id']));
		$coo_data_array = $coo_data->get_data_objects_array();
		return count($coo_data_array);
	}
	
	function get_value_in_combis_count($p_properties_id, $p_properties_values_id){
		$t_properties_id = (int)$p_properties_id;
		$t_properties_values_id = (int)$p_properties_values_id;
		
		$coo_data = new GMDataObjectGroup('products_properties_index', array('properties_id' => $t_properties_id, 'properties_values_id' => $t_properties_values_id, 'language_id' => $_SESSION['languages_id']));
		$coo_data_array = $coo_data->get_data_objects_array();
		return count($coo_data_array);
	}
}
?>